<?php

// +----------------------------------------------------------------------
// |供应商服务层
// +----------------------------------------------------------------------

namespace app\admin\system;
use think\facade\Db;

/**
 * 供应商
 */
class SupplierService
{ 
    /**
    * 获取的供应商分组的数据
    * @param array $param 前台传过来的参数  
    * @return array
    */
   public static function   getlist($param){
      
       $params= $param['params'];  
       $field='id,name,code,status,username,tel,createtime';    
        $where = [];    
        if($params['name']){
            $where[] = ['name', 'like', '%'. $params['name']. '%'];
        }
        if($params['code']){
            $where[] = ['code', 'like', '%'. $params['code']. '%'];
        }
        if($params['status']==2){
            $where[] = ['status', '=', 0];
        }else  if($params['status']==1){
            $where[] = ['status', '=', 1];
        } 
        $Supplier_list = Db::name('Supplier')->field($field)->where($where)->order('id asc,status desc')->paginate(['list_rows'=>$params['pagesize'],'page'=>$params['pagenum']])->toArray();    
        $data['list'] = $Supplier_list['data'];
        $data['total'] = $Supplier_list['total'];    
        return $data; 
   }

        /**
     * 修改菜单是否为菜单
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function create($param){
        $params= $param['params'];  
   
        $data=[
            'tel'=> $params['tel'],
            'status'=> $params['status'],
            'name' => $params['name'],
            'code' =>strtoupper(get_first_letter($params['name'])),//获取中文的首字母，并且转为大写
            'username' =>  $params['username'],  
        ];

        if($params['id']){
               //有值则说明是修改 
               $result =  Db::name('Supplier')->where('id',$params['id'])->update($data);    
        }else{
            //执行添加数据
            $data['createtime']=datetime();
            $data['createymd']=(int)date('Ymd',time());
            $result =  Db::name('Supplier')->save($data);
        }
        if($result){
            return true;
        }else{
            error('修改失败');
        }
       
    }

}